﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SRQIDT.forms
{
    public partial class AddFailureMode : Form
    {
        public AddFailureMode()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
                MessageBox.Show("故障模式输入有误！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            else if ((double.Parse(textBox4.Text) >= 1.0) || (double.Parse(textBox4.Text) <= 0.0))
                MessageBox.Show("故障概率输入有误！", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            else
            {
                //添加到listview
                ListViewItem LVFM = new ListViewItem();
                LVFM.Text = Form1.TS_Name.ToString();
                LVFM.SubItems.Add(textBox1.Text);
                LVFM.SubItems.Add(textBox2.Text);
                LVFM.SubItems.Add(textBox3.Text);
                LVFM.SubItems.Add(textBox4.Text);

                Form1.FM_count++;
                //添加到失效模式数组(从0开始计数)
                int i = (Form1.FM_count - 1);
                int j = (Form1.TS_count - 1);
                Form1.FMA[i] = new Form1.FailureMode();
                Form1.FMA[i].TS_Name = Form1.TSA[j].TS_Name;
                Form1.FMA[i].FM_id = textBox1.Text;
                Form1.FMA[i].Level = int.Parse(textBox2.Text);
                Form1.FMA[i].Recovery_time = double.Parse(textBox3.Text);
                Form1.FMA[i].Probability = double.Parse(textBox4.Text);
                Form1.FMA[i].FailureRate = -(Math.Log(1 - Form1.FMA[i].Probability) / Form1.TSA[j].Excuse_Time);
                Form1.FMA[i].FM_isdeleted = false;
                //添加失效率
                LVFM.SubItems.Add(Form1.FMA[i].FailureRate.ToString());
                Form1.new_TaskModeling.AddFMListItem(LVFM);
                if (j == 0)
                    Form1.TSA[j].FailureModeCount = 1;
                else
                    Form1.TSA[j].FailureModeCount++;
                this.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            this.Close();
        }
    }
}
